Documentation for Users  2.0.6
Perception Toolbox for Virtual Reality (PTVR) Manual
result_file_post_treatment.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pandas
3 import glob
4 import os
5 from pathlib import Path
6 from datetime import datetime
7 import math
8 import numpy as np
9 #import eye_tracking_analysis as eyetracking
10 #import gaze_path_visualization as visualize
11 #import mnread_vr_parameters as parameters
12 
13 # =============================================================================
14 # PARAMETERS #
15 # =============================================================================
16 
17 text_description ="""
18 goal : Post treatment of the result file obtained after an experiment
19 
20 how to use : Call this script at the end of your PTVR experiment
21 
22 predictions :
23 
24 Created on : Thu Oct 6 11:50:47 2022
25 @author:jdelacha
26 """
27 
28 
29 root_folder = str(Path(__file__).parents[4])
30 root_folder = root_folder.replace('\\','/' )
31 my_path = root_folder + "/PTVR_Operators/Results/mnread_vr_experiment/"
32 # TO DO : ajouter qu'on ne traite que le _Main_ !!
33 list_of_files = glob.glob(my_path + "*Main*.csv") # * means all if need specific format then *.csv
34 #list_of_files = glob.glob(my_path + "*Subject6*.csv")
35 result_file_name = max(list_of_files, key=os.path.getctime)
36 
37 
38 columns_to_keep = ['callback_name','callback_timestamp','sentence_number',
39  'sentence_displayed','x_logmar','viewing_distance_in_m',
40  'misread_words','sentence_tag','tangent_screen_id',
41  'tangent_screen_width_in_m','tangent_screen_height_in_m'] #
42 
43 final_columns = ['sentence_number','sentence_tag','sentence_displayed',
44  'tangent_screen_id','tangent_screen_width_in_m','tangent_screen_height_in_m',
45  'x_logmar','viewing_distance_in_m','misread_words','errors_number', 'is_outside_area_radius','trial_ignored']
46 
47 path_to_new_file = result_file_name+ "_cleaned.csv"
48 
49 # =============================================================================
50 # END PARAMETERS #
51 # =============================================================================
52 
53 def correct_date_format(string):
54  string = datetime.strptime(string, "%Y-%m-%d-%H-%M-%S.%f")
55  return string
56 
57 def count_words(string):
58  if(isinstance(string, float)):
59  return 0
60  else :
61  return len(str(string).split())
62 
63 def main():
64  result_file = pandas.read_csv(result_file_name, sep=";", usecols=columns_to_keep, encoding="utf_8")
65  result_file = result_file.loc[(result_file['callback_name']=='EndCurrentScene') | (result_file['callback_name']=='AddNewPoint') | (result_file['callback_name']=='IgnoreTrial') | (result_file['callback_name']=='OutsideAreaLimit')]
66 
67 
68 
69  result_file['callback_timestamp'] = result_file['callback_timestamp'].apply(correct_date_format)
70 
71  result_file['reading_time'] = result_file['callback_timestamp'].diff().dt.total_seconds()
72 
73  result_file['is_outside_area_radius'] = "No"
74  result_file['trial_ignored'] = "No"
75 
76  print(result_file)
77 
78 
82 
83  if 'IgnoreTrial' in result_file.values:
84  values = result_file[result_file['callback_name']=='IgnoreTrial']['sentence_number'].tolist()
85  for value in values :
86  result_file.loc[result_file.sentence_number == value, 'trial_ignored'] = "Yes"
87 
88 
90  if 'OutsideAreaLimit' in result_file.values:
91  numbers = result_file[result_file['callback_name']=='OutsideAreaLimit']['sentence_number'].tolist()
92  for number in numbers :
93  result_file.loc[result_file.sentence_number == number, 'is_outside_area_radius'] = "Yes"
94 
95 
96 
97  result_file['errors_number'] = result_file['misread_words'].apply(count_words)
98 
99 
100 
101 
102  temp_frame = pandas.DataFrame(result_file)
103  temp_frame = temp_frame.loc[(temp_frame['callback_name']=='AddNewPoint')]
104  temp_frame = temp_frame[['reading_time','sentence_number']]
105 
106 
107 
108 
109  result_file = result_file.loc[(result_file['callback_name']=='EndCurrentScene')]
110  result_file.drop(index=result_file.index[0], axis=0, inplace=True)
111  result_file.drop(columns=["reading_time"], axis=1, inplace=True)
112 
113 
114 
115  result_file = result_file[final_columns]
116  final_frame = pandas.merge(result_file,temp_frame,on=["sentence_number"])
117  print(final_frame)
118 
119  final_frame.to_csv(path_to_new_file, sep=";", index = False, header=True)
120  print("New csv written")
121 # =============================================================================
122 # eyetracking.called_function(_result_cleaned_file_name = path_to_new_file)
123 # # =============================================================================
124 # # if (parameters.record_eye_gaze):
125 # # visualize.called_function(_result_cleaned_file_name = path_to_new_file)
126 # # =============================================================================
127 #
128 #
129 # =============================================================================
130 if __name__ == "__main__":
131  main()
def count_words(string)
def correct_date_format(string)
def main()